package com.blockchain.front.servlets;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.framework.http.session.Session;
import com.blockchain.framework.http.session.SessionManager;
import com.blockchain.framework.http.session.authentication.AuthenticationException;
import com.blockchain.framework.http.session.authentication.VerifyCodeAuthentication;
import com.blockchain.framework.service.ServiceSession;

public class TxYzmExists extends AbstractFrontServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected boolean mustAuthenticated() {
		return false;
	}

	@Override
	protected void processGet(HttpServletRequest request, HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		processPost(request, response, serviceSession);
	}

	@Override
	protected void processPost(HttpServletRequest request, HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		response.setContentType("text/html;charset=" + getResourceProvider().getCharset());
		Session session = serviceSession.getSession();
		if (session != null && session.isAuthenticated()) {
			sendRedirect(request, response, getController().getViewURI(request, Index.class));
			return;
		}
		if (session == null) {
			session = getResourceProvider().getResource(SessionManager.class).getSession(request, response, true);
		}
		PrintWriter out = response.getWriter();
		String txverifyCode = request.getParameter("txverifyCode");
		VerifyCodeAuthentication authentication = new VerifyCodeAuthentication();
		authentication.setVerifyCodeType("ZC");
		authentication.setVerifyCode(txverifyCode);
		try {
			session.authenticateVerifyCode(authentication);
			out.write("1");
		} catch (AuthenticationException e) {
			out.write("无效的验证码或验证码已过期。");
		}
	}

}
